package com.yimi.yinhepda.views.moveStock_module.unload;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.yimi.yinhepda.R;
import com.yimi.yinhepda.adapter.RecycleAdapter.MoveStockUnloadTaskAdapter;
import com.pda.ym.base.CommonViewHolder;
import com.pda.ym.base.BaseTitleActivity;
import com.pda.ym.callbacks.UICallBack;
import com.yimi.yinhepda.db.table.MoveStockTaskCacheTable;
import com.yimi.yinhepda.entity.bean.MoveStockTaskEntity;
import com.yimi.yinhepda.entity.request.QueryMoveStockTaskRequest;
import com.yimi.yinhepda.entity.response.MoveStockTaskResponse;
import com.pda.ym.net.NetRequest;
import com.yimi.yinhepda.utils.Common;
import com.pda.ym.utils.Constants;
import com.pda.ym.utils.SoundVibratorManager;
import com.yimi.yinhepda.views.load_module.LoadUnloadGroupActivity;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: lj
 * Date: 2018/12/25
 * Description: 移库卸车任务集界面
 */
public class MoveStockUnloadTaskActivity extends BaseTitleActivity implements CommonViewHolder.onItemCommonClickListener {

    private RecyclerView rvCacheList;
    private MoveStockUnloadTaskAdapter mCacheAdapter;
    private List<MoveStockTaskCacheTable> mCacheList;
    private MoveStockTaskCacheTable mCacheTableEntity;
    public static Handler mHandler;

    @Override
    public String getPageName() {
        return "移库卸车任务集";
    }

    @Override
    public int setContentViewId() {
        return R.layout.activity_move_stock_unload_cache;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        rvCacheList = findViewById(R.id.rv_cache_list);
        findViewById(R.id.iv_back).setOnClickListener(this);
        findViewById(R.id.iv_flash).setOnClickListener(this);
        findViewById(R.id.iv_exec).setOnClickListener(this);

        mCacheList = new ArrayList<>();
        mCacheAdapter = new MoveStockUnloadTaskAdapter(this, mCacheList, this);
        rvCacheList.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        rvCacheList.setAdapter(mCacheAdapter);

        loadData();
    }

    @SuppressLint("HandlerLeak")
    @Override
    protected void onStart() {
        super.onStart();
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                loadData();
            }
        };
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_flash:
                loadData();
                break;
            case R.id.iv_exec:
                if (mCacheTableEntity == null || !mCacheTableEntity.isCheck()) {
                    showToast("请选择一项卸车任务！");
                    SoundVibratorManager.playSound(2);
                    return;
                }
                Intent intent = new Intent(this, LoadUnloadGroupActivity.class);
                //扫描类型：1-装车扫描，2=卸车扫描 ,3-离线装车，4-离线卸车,5-移库装车，6-移库卸车
                intent.putExtra("scanType", 6);
                intent.putExtra("CacheTableEntity", mCacheTableEntity);
                startActivity(intent);
                break;
            default:
                break;
        }
    }

    @Override
    public void onItemClickListener(int position) {
        if (position == RecyclerView.NO_POSITION) {
            return;
        }
        mCacheTableEntity = mCacheList.get(position);
        if (mCacheTableEntity.isCheck()) {
            mCacheTableEntity.setCheck(false);
        } else {
            for (MoveStockTaskCacheTable item : mCacheList) {
                if (item.equals(mCacheTableEntity)) {
                    item.setCheck(true);
                } else {
                    item.setCheck(false);
                }
            }
        }
        mCacheAdapter.notifyDataSetChanged();
    }

    @Override
    public void onItemLongClickListener(int position) {

    }

    /**
     * 移库卸车任务获取
     */
    private void loadData() {
        showPgDlg("加载数据中...");
        QueryMoveStockTaskRequest request = new QueryMoveStockTaskRequest();
        request.setDeptCode(sharedPreferUtil.getDeptCode());
        request.setTransTypes(new Integer[]{2, 3});
        new NetRequest().setMethod(NetRequest.Method.POST_STRING)
                .setResponsClazz(MoveStockTaskResponse.class)
                .setUrl(Constants.URL_QUERY_MOVE_STOCK_TASK)
                .setRequestObject(request)
                .setConnTimeOut(5 * 1000)
                .execute(new UICallBack() {
                    @Override
                    public void onError(String msg) {
                        dismissPgDlg();
                        showToast(msg);
                        SoundVibratorManager.playSound(2);
                    }

                    @Override
                    public void onSuccess(Object obj) {
                        dismissPgDlg();
                        mCacheList.clear();
                        MoveStockTaskResponse response = (MoveStockTaskResponse) obj;
                        List<MoveStockTaskEntity> taskEntityList = response.getData();
                        for (MoveStockTaskEntity item : taskEntityList) {
                            MoveStockTaskCacheTable cacheTable = new MoveStockTaskCacheTable();
                            cacheTable.setDeptCode(sharedPreferUtil.getDeptCode());
                            cacheTable.setOperCode(sharedPreferUtil.getValue(Common.USER_CODE));
                            cacheTable.setTaskNo(item.getTaskNo());
                            cacheTable.setMoveOutCode(item.getLoadAreaCode());
                            cacheTable.setMoveOutName(item.getLoadAreaName());
                            cacheTable.setMoveInCode(item.getUnloadAreaCode());
                            cacheTable.setMoveInName(item.getUnloadAreaName());
                            cacheTable.setCarNo(item.getCarNo());
                            cacheTable.setPiaoNum(item.getUnloadPieceNum());
                            cacheTable.setOperType(2);
                            cacheTable.setPlatformNo(item.getUnloadPlantformNo());
                            mCacheList.add(cacheTable);
                        }
                        mCacheAdapter.notifyDataSetChanged();
                    }
                });

    }

    @Override
    public boolean showBack() {
        return false;
    }

    @Override
    public void setBack() {

    }

    @NotNull
    @Override
    public String setTitle() {
        return getPageName();
    }

}
